package com.nbjtjc.safe.api.sys;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysRoleMapper;
import com.nbjtjc.safe.model.sys.SysRole;
import com.nbjtjc.safe.service.sys.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 系统角色
 */
@RestController
@RequestMapping("/role")
@CrossOrigin("*")
public class SysRoleController {
    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping(value = "/detail",method = {RequestMethod.POST})
    public Result<SysRole> detail(int roleId){
        Result<SysRole> result=new Result<>();
        try {
            SysRole role=new SysRole();
            role=sysRoleService.detail(roleId);
            result.setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/find",method = {RequestMethod.POST})
    public Result<PageInfo> find(SysRole sysRole, Page page){
        Result<PageInfo> result=new Result<>();
        try {
            PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
            List<SysRole> roles=roleMapper.search(sysRole);
            result.setData(new PageInfo(roles));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    public Result<SysRole> update(SysRole role){
        Result<SysRole> result=new Result<>();
        try {
            role=sysRoleService.update(role);
            result.setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/add",method = {RequestMethod.POST})
//    @RequiresPermissions("sys:role:add")
    public Result<SysRole> add(SysRole role){
        Result<SysRole> result=new Result<>();
        try {
            if(role==null){
                result.setFalse("不能为空");
                return result;
            }
            role=sysRoleService.add(role);
            result.setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
//    @RequiresPermissions("sys:role:delete")
    public Result<Boolean> delete(int id){
        Result<Boolean> result=new Result<>();
        try {
            if(id==0){
                result.setFalse("不能为空");
                return result;
            }
            roleMapper.deleteByPrimaryKey(id);
            result.setData(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }
}
